﻿using System.Collections.Generic;
using System.Windows.Forms;
using ApplicationLibrary.ExtendedControls;
using ApplicationLibrary.SqlConnector;

namespace Clothes.Sales.ModuleData
{
    internal class RadarSalesParam : RadarParameters
    {
        public RadarSalesParam(SalesType saletype, string code)
        {
            Params = new List<object>();
            Params.Add(saletype);
            Params.Add(code);
        }
    }

    internal class RadarCounterSalesParam : RadarParameters
    {
        public RadarCounterSalesParam(string code)
        {
            Params = new List<object>();
            Params.Add(code);
        }
    }

    #region Radar Sale

    internal class RadarSale : RadarForm
    {
        private QueryBuilder qb = new QueryBuilder();
        private SqlABParameter p1,p2,p3;
        SalesType SalesType;

        public RadarSale(SalesType saletype)
            : base()
        {
            SalesType = saletype;

            rdrCodeColumn = CL_Sales.DocumentNo.Name;
            rdrDescColumn = CL_Sales.Description.Name;
            rdrNameSpace = "Clothes.Clothes.Sales.Forms.Invoice";
        }

        protected override Table GetRadarTable()
        {
            return new CL_Sales();
        }

        protected override bool DefineFindQuery(SqlABCommand sqlCmd)
        {
            p1 = new SqlABParameter("@p1", CL_Sales.SaledocType);
            p2 = new SqlABParameter("@p2", CL_Sales.DocumentNo);

            qb.Clear();
            qb.AddSelectAll(CL_Sales.Name);
            qb.AddFrom(CL_Sales.Name);
            qb.AddCompare(CL_Sales.SaledocType, p1);
            qb.AddCompare(CL_Sales.DocumentNo,  p2);

            sqlCmd.CommandText = qb.Query;
            sqlCmd.Parameters.Add(p1);
            sqlCmd.Parameters.Add(p2);

            return true;
        }

        protected override void PrepareFindQuery(IRadarParameters param)
        {
            p1.Value = param.Params[0];
            p2.Value = param.Params[1];
        }

        protected override void OnFound(SqlABDataReader sqlReader)
        {
            Description = sqlReader[CL_Sales.Description.Name].ToString();
        }

        protected override string DefineBrowseQuery(SqlABCommand sqlCmd, string findQuery)
        {
            p3 = new SqlABParameter("@p3", CL_Sales.SaledocType);
            p3.Value = SalesType;

            qb.Clear();
            qb.AddSelectAll(CL_Sales.Name);
            qb.AddFrom(CL_Sales.Name);
            qb.AddCompare(CL_Sales.SaledocType, p3);
            qb.AddFilter(findQuery);

            sqlCmd.Parameters.Add(p3);

            return qb.Query;
        }

        protected override void PrepareBrowseQuery()
        {
        }

        protected override IRadarParameters PrepareRadarParameters(DataGridViewRow row)
        {
            return new RadarSalesParam(SalesType, DataGridViewRowValues.GetValue<string>(row, CL_Sales.DocumentNo));
        }

        public override IRadarParameters GetRadarParameters(string text)
        {
            return new RadarSalesParam(SalesType, text);
        }

        public override string GetCodeFromParameters(IRadarParameters param)
        {
            return param.GetValue<string>(1);
        }

        //protected override void FormatColumns(DataGridViewRow row, string columnName)
        //{
        //    if (columnName == FG_Athlete.Club.Name && row.Cells[0].Value != null)
        //    {
        //        string mail = DataGridViewRowValues.GetValue<string>(row, FG_Athlete.Mail);
        //        if (DRClub.Find(mail))
        //            DataGridViewRowValues.SetValue<string>(row, FG_Athlete.Club, DRClub.GetValue<string>(FG_Club.Club));
        //    }
        //}

    }

    #endregion

    #region Radar CounterSales

    internal class RadarCounterSales : RadarForm
    {
        private QueryBuilder qb = new QueryBuilder();
        private SqlABParameter p1, p2;

        public RadarCounterSales()
            : base()
        {
            rdrCodeColumn = CL_CounterSales.DocumentNo.Name;
            rdrNameSpace = "Clothes.Clothes.Sales.Forms.CounterSales";
        }

        protected override Table GetRadarTable()
        {
            return new CL_CounterSales();
        }

        protected override bool DefineFindQuery(SqlABCommand sqlCmd)
        {
            p1 = new SqlABParameter("@p1", CL_CounterSales.DocumentNo);

            qb.Clear();
            qb.AddSelectAll(CL_CounterSales.Name);
            qb.AddFrom(CL_CounterSales.Name);
            qb.AddCompare(CL_CounterSales.DocumentNo, p1);

            sqlCmd.CommandText = qb.Query;
            sqlCmd.Parameters.Add(p1);

            return true;
        }

        protected override void PrepareFindQuery(IRadarParameters param)
        {
            p1.Value = param.Params[0];
        }

        protected override void OnFound(SqlABDataReader sqlReader)
        {
            Description = "";
        }

        protected override string DefineBrowseQuery(SqlABCommand sqlCmd, string findQuery)
        {
            qb.Clear();
            qb.AddSelectAll(CL_CounterSales.Name);
            qb.AddFrom(CL_CounterSales.Name);
            qb.AddFilter(findQuery);

            return qb.Query;
        }

        protected override void PrepareBrowseQuery()
        {
        }

        protected override IRadarParameters PrepareRadarParameters(DataGridViewRow row)
        {
            return new RadarCounterSalesParam(DataGridViewRowValues.GetValue<string>(row, CL_Sales.DocumentNo));
        }

        public override IRadarParameters GetRadarParameters(string text)
        {
            return new RadarCounterSalesParam(text);
        }

        public override string GetCodeFromParameters(IRadarParameters param)
        {
            return param.GetValue<string>(0);
        }

        //protected override void FormatColumns(DataGridViewRow row, string columnName)
        //{
        //    if (columnName == FG_Athlete.Club.Name && row.Cells[0].Value != null)
        //    {
        //        string mail = DataGridViewRowValues.GetValue<string>(row, FG_Athlete.Mail);
        //        if (DRClub.Find(mail))
        //            DataGridViewRowValues.SetValue<string>(row, FG_Athlete.Club, DRClub.GetValue<string>(FG_Club.Club));
        //    }
        //}

    }

    #endregion
}